﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace td.Core.DiscoverAddins
{
    public partial class PadList : DevExpress.XtraEditors.XtraUserControl
    {
        td.Core.Xml.AddIn addin;
        public PadList(td.Core.Xml.AddIn addin)
        {
            InitializeComponent();

            this.addin = addin;
            this.listBoxControl1.SelectedIndexChanged += new EventHandler(listBoxControl1_SelectedIndexChanged);
            this.UpDateListInfo();
        }
        void UpDateListInfo()
        {
            this.listBoxControl1.Items.Clear();
            for (int i = 0; i < addin.Pads.Count; i++)
            {
                this.listBoxControl1.Items.Add(addin.Pads[i].Title);
            }
        }

        void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetValue();
        }

        private void SetValue()
        {
            if (this.listBoxControl1.Items.Count <= 0) return;
            var t = addin.Pads[this.listBoxControl1.SelectedIndex];
            this.tbTitle.Text = t.Title;
            this.tbClass.Text = t.Class;
            this.tbguid.Text = t.guid;
            this.tbicon.Text = t.Icon;
            this.tbdescription.Text = t.Description;

             
            if (t.ShowAutoHideButton != null)
            {
                this.cbShowHideButton.EditValue = t.ShowAutoHideButton.ToLower() == "true" ? true : false;
            }
            if (t.ShowCloseButton != null)
            {
                this.cbShowCloseButton.EditValue = t.ShowCloseButton.ToLower() == "true" ? true : false;
            }
        }
        //新建按钮
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ClearInfo();
        }

        private void ClearInfo()
        {
            this.tbTitle.Text ="";
            this.tbClass.Text = "";
            this.tbguid.Text = "";
            this.tbicon.Text = "";
            this.tbdescription.Text = "";
            this.cbShowCloseButton.EditValue = false;
            this.cbShowHideButton.EditValue = false;
        }

        private void sbSave_Click(object sender, EventArgs e)
        {
            addin.Pads[this.listBoxControl1.SelectedIndex] = SaveValue();
            UpDateListInfo();
        }

        private td.Core.Xml.PadXml SaveValue()
        {
            td.Core.Xml.PadXml t = new Xml.PadXml();
            t.guid = this.tbguid.Text;
            t.Class = this.tbClass.Text;
            t.Description = this.tbdescription.Text;
            t.Icon = this.tbicon.Text;
            t.Title = this.tbTitle.Text;

            t.ShowAutoHideButton = this.cbShowHideButton.EditValue.ToString();
            t.ShowCloseButton = this.cbShowCloseButton.EditValue.ToString();
            return t;
        }

        private void sbNew_Click(object sender, EventArgs e)
        {
            addin.Pads.Add(SaveValue());
            this.listBoxControl1.Items.Clear();
            UpDateListInfo();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
           
            Reflection r = new Reflection(addin);
           
          
            if (r.ShowDialog() == DialogResult.OK)
            {
                this.tbClass.Text = r.Result;
            }

        }

        private void btnAddGuid_Click(object sender, EventArgs e)
        {
            this.tbguid.Text = Guid.NewGuid().ToString() ;
        }
        //设置图标
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.png|*.png|*.jpg|*.jpg|*.bmp|*.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                this.tbicon.Text = open.FileName.Replace(Application.StartupPath + "\\","");
            }
        }
        //删除按钮
        private void sbdel_Click(object sender, EventArgs e)
        {
            if (this.listBoxControl1.SelectedItem != null)
            {
                addin.Pads.RemoveAt(this.listBoxControl1.SelectedIndex);
                UpDateListInfo();
            }
        }
    }
}
